<?php

namespace app\admin\controller;

use think\Request;
use think\Db;
use think\Response;
use think\Session;
use think\Cookie;


class admin extends Login
{
    // public function _initialize()
    // {
    //     $request = Request::instance();
    //     // $this->error("抱歉！没有操作权限！");

    //     (var_dump($request->module(),$request->controller(),$request->action()));
        
    // }
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index($page = 1)
    {
//        return dump($request->post());
        //查询gqx_admin,role,user_role表
        $count = Db::name('admin')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }

        $data = Db::name('user_role ur')->join('gqx_role r','r.id=ur.role_id')->join('gqx_admin a','ur.user_id=a.id','right')->order(['a.id'=>'asc'])->page($page,10)->select();
        return view('admin/admin',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        $data = Db::name('role')->select();
        // return dump($data);
        return view('admin/add',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data
        ]);
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
        // return $request->param()['pass'];
        // return $request->param();
        // return var_dump($request->param());
        if($request->param()['pass'] != $request->param()['repass']){
            return false;
        }
        if(@$request->param()['status'] == 'on'){
            $data['status'] = 1;
        }else{
            $data['status'] = 0;
        }
        $data['name'] = $request->param()['name'];
        $data['password'] = md5($request->param()['pass']);
        // return($data);
        $res = Db::name('admin')->insert($data);
        if($res != 1){
            return '操作失败';
        }
        // 判断是否提交对应角色
        if(@empty($request->param()['role'])){
            return $res;
        }

        $arr['user_id'] = Db::name('user')->getLastInsID();
        $arr['role_id'] = $request->param()['role'];
        return Db::name('user_role')->insert($arr);


    }

    /**
     * 显示指定的搜索资源
     *
     * @param  int  $like
     * @return \think\Response
     */
    public function search($page = 1,Request $request)
    {
        $post = $request->post();
        $count = Db::name('admin')->where('name','like','%'.$post['like'].'%')->count('id');
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('user_role ur')->join('gqx_role r','r.id=ur.role_id')->join('gqx_admin a','ur.user_id=a.id','right')->order(['a.id'=>'asc'])->where('a.name','like','%'.$post['like'].'%')->page($page,10)->select();
        return view('admin/admin',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'data'=>$data,
            'count'=>$count,
            'page'=>$page,
            'admin_name'=>Session::get('admin_name'),
            'admin_id'=>Session::get('admin_id')
        ]);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        // return $id;
        $arr = Db::name('admin')->where('id',$id)->find();
        $data = Db::name('role')->select();
        $rid = Db::name('user_role')->where('user_id',$id)->find()['role_id'];
        // var_dump($arr);
        // return ;
        return view('admin/edit',['CSS_PATH'=>config('admin_css'),
            'JS_PATH'=>config('admin_js'),
            'ICON_PATH'=>config('admin_icon'),
            'IMG_PATH'=>config('admin_img'),
            'arr'=>$arr,
            'data'=>$data,
            'rid'=>$rid
        ]);
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
        // return $id;
        // return $request->param();
        // 直接处理状态更改请求
        if(@$request->param()['source'] == 'status'){
            $data['status'] = $request->param()['status'];
            return Db::name('admin')->where('id',$id)->update($data);
        }
        //正常处理编辑流程
        $arr = $request->param();

        (@empty($arr['pass'])) ? : $data['password']=md5($arr['pass']) ;
        (@empty($arr['name'])) ? : $data['name']=$arr['name'] ;
        $data['status']= (@$arr['status']=='on' ? 1 : 0) ;
        
        // return $data;
        $u = Db::name('admin')->where('id',$id)->update($data);
        $str = $u ? '管理员信息更新成功；' : '管理员信息未更改；';

        // 处理对应角色信息
        if($arr['role_ls'] != $arr['role']){
            $role['role_id'] = $arr['role'];
            $role['user_id'] = $id;
            if(empty($arr['role_ls'])){
                $r = Db::name('user_role')->insert($role); 
            }else{
                $r = Db::name('user_role')->update($role);
            }
        }

        $str .= @$r ? '角色对应更新成功；' : '角色对应未更改；';

        return $str;

    }


    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
        return Db::name('admin')->where('id',$id)->delete();
    }
}
